package com.hpin.fansea.service;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.adapter.HouseRentAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.widget.ActionSheetDialog;

/**
 *
 * 房屋租售
 * Created by Aaron on 15/9/16.
 */
public class HouseRentActivity extends BaseActivity {

    private HouseRentAdapter mAdapter;
    private TextView tv_sale;
    private TextView tv_rent;
    private PullToRefreshListView plv_rent;

    private int currentStyle = 0;//当前显示的是出售还是出租 0,出售  1,出租
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_house_rent);

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(getResources().getString(R.string.house_rent));
        tv_right.setText(getResources().getString(R.string.release));

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        plv_rent = (PullToRefreshListView) findViewById(R.id.plv_rent);

        mAdapter = new HouseRentAdapter(this);
        plv_rent.setAdapter(mAdapter);

        tv_sale = (TextView) findViewById(R.id.tv_sale);
        tv_rent = (TextView) findViewById(R.id.tv_rent);

        tv_sale.setOnClickListener(this);
        tv_rent.setOnClickListener(this);

        plv_rent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ++position;

                if (currentStyle == 0) {//出售详情

                    mIntent = new Intent(HouseRentActivity.this, SaleDetailActivity.class);

                }else {                 //出租详情

                    mIntent = new Intent(HouseRentActivity.this, RentDetailActivity.class);
                }

                startActivity(mIntent);

            }
        });

        plv_rent.setMode(PullToRefreshBase.Mode.BOTH);

        plv_rent.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lastUpdate(refreshView);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;
            case R.id.tv_sale://出售


                currentStyle = 0;
                tv_sale.setBackgroundResource(R.drawable.shape_rent_green_bg_left);
                tv_rent.setBackgroundResource(R.drawable.shape_rent_white_bg_right);

                tv_sale.setTextColor(getResources().getColor(R.color.white));
                tv_rent.setTextColor(getResources().getColor(R.color.text_color));

                break;
            case R.id.tv_rent://出租

                currentStyle = 1;
                tv_sale.setBackgroundResource(R.drawable.shape_rent_white_bg_left);
                tv_rent.setBackgroundResource(R.drawable.shape_rent_green_bg_right);

                tv_rent.setTextColor(getResources().getColor(R.color.white));
                tv_sale.setTextColor(getResources().getColor(R.color.text_color));

                break;

            case R.id.tv_right: //发布

                showChoose();

                break;

        }
    }

    /**
     * 弹出选择照片的对话框
     */
    protected void showChoose() {

        new ActionSheetDialog(this)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("我要出售", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 出售
                                Intent mIntent = new Intent(HouseRentActivity.this, SaleReleaseActivity.class);
                                startActivity(mIntent);

                            }

                        })
                .addSheetItem("我要出租", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 出租

                                Intent mIntent = new Intent(HouseRentActivity.this, RentReleaseActivity.class);
                                startActivity(mIntent);

                            }
                        }).show();

    }

    private void lastUpdate(PullToRefreshBase<ListView> refreshView) {

        String label = DateUtils.formatDateTime(HouseRentActivity.this, System.currentTimeMillis(),
                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

        refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

    }

}
